<template>
  <div class="scheme-content table" v-loading="loading">
    <div class="btn">
      <el-button type="primary" @click="changeAddProject">创建作战任务</el-button>
    </div>
    <el-table :data="tableData" border resizable style="width: 100%" :height="maxheight">
      <el-table-column label="任务名称" prop="taskName">
        <template slot-scope="scope">
          <span @click="changeProjectDetail(scope.row)" class="detail-span">{{
            scope.row.taskName
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="任务类型" prop="taskType"></el-table-column>
      <el-table-column label="创建人" prop="createdUser"></el-table-column>
      <el-table-column label="创建时间" prop="createdTime"></el-table-column>
      <el-table-column label="操作" width="120">
        <template slot-scope="{ row }">
          <el-button size="mini" @click="changeEditProject(row)" type="text">编辑</el-button>
          <el-button size="mini" type="text" @click="handleDelete(row)">删除</el-button>
          <el-button size="mini" type="text" @click="recommendScheme(row)">推荐</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination style="padding-top: 24px" @size-change="handleSizeChange" @current-change="handleCurrentChange"
      :current-page="pageData.page" :page-sizes="[10, 20, 30, 50]" :page-size="pageData.pageSize"
      layout="total, sizes, prev, pager, next" :total="pageData.total">
    </el-pagination>
    <project-dialog :projectDialog="projectDialog" :form="form" @closeDialog="closeDialog"
      @changeoperate="changeoperate"></project-dialog>
  </div>
</template>

<script>
import ProjectDialog from "../project/component/ProjectDialog.vue";
import { getTaskList, deleteTask, saveRecommendPlan } from "../../api/project";
export default {
  components: { ProjectDialog },
  data() {
    return {
      tableData: [],
      maxheight: "calc(100vh - 238px)",
      loading: false,
      form: {},
      projectDialog: false,
      pageData: {
        page: 1,
        size: 10,
        total: 0,
      },
    };
  },
  mounted() {
    this.getData();
  },

  methods: {
    convertTaskType(val) {
      let taskType = null;
      switch (val) {
        case 0:
          taskType = "侦察";
          break;
        case 1:
          taskType = "干扰";
          break;
        case 2:
          taskType = "打击";
          break;
        case 3:
          taskType = "发现";
          break;
        case 4:
          taskType = "跟踪";
          break;
        case 5:
          taskType = "定位";
          break;
        case 6:
          taskType = "评估";
          break;
      }
      return taskType;
    },
    getData() {
      const list = {
        page: this.pageData.page,
        size: this.pageData.size,
      };
      this.loading = true;
      getTaskList(list)
        .then((res) => {
          const { code, data, msg } = res?.data;
          if (code == 200) {
            data.records.forEach(item => {
              item.taskType = this.convertTaskType(item.taskType);
            })
            this.tableData = data.records;
            this.pageData.total = data.total;
          } else {
            this.$message.error(msg);
          }
          this.loading = false;
        })
        .catch((err) => {
          this.$message.error(err);
          this.loading = false;
        });
    },
    changeoperate() {
      this.getData();
    },
    handleSizeChange(val) {
      this.pageData.pageSize = val;
      this.pageData.page = 1;
      this.getData();
    },
    handleCurrentChange(val) {
      this.pageData.page = val;
      this.getData();
    },
    changeProjectDetail(row) {
      localStorage.setItem("taskId", row.taskId);
      this.$router.push("/app/projectDetail/SchemeDesign");
    },
    changeAddProject() {
      this.$router.push("/app/createTask");
    },
    changeEditProject(row) {
      localStorage.setItem("taskId", row.taskId);
      this.$router.push("/app/editTask");
    },
    closeDialog() {
      this.projectDialog = false;
    },
    recommendScheme(row) {
      this.loading = true;
      saveRecommendPlan({ taskId: row.taskId })
        .then((res) => {
          const { code, data, msg } = res?.data;
          if (code == 200) {
            this.$router.push("/app/projectDetail/SchemeDesign");
          } else {
            this.$message.error(msg);
          }
          this.loading = false;
        })
        .catch((err) => {
          this.$message.error(err);
          this.loading = false;
        });

    },
    handleDelete(row) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "error",
      })
        .then(() => {
          deleteTask(row.taskId)
            .then((res) => {
              const { code, msg } = res?.data;
              if (code == 200) {
                this.$message.success(msg);
                this.getData();
              } else {
                this.$message.error(msg);
              }
            })
            .catch((err) => {
              this.$message.error(err);
            });
        })
        .catch(() => { });
    },
  },
};
</script>
<style lang="scss" scoped>
.detail-span {
  color: #07f;
  cursor: pointer;
}
</style>
